/**
 * Simple page navigation with JQMultiTouch
 * 
 * Note: axis is inverted (swipe left in order to move right)
 */
var pages = [ 'index.html', 'article.html', 'gallery.html' ];

function getCurrentPage() {
	var url = window.location.pathname;
	return url.substring(url.lastIndexOf('/') + 1);
}

function getPage(currentPage, offset) {
	var N = pages.length;
	return pages[(pages.indexOf(currentPage) + offset + N) % N];
}

function getNextPage(currentPage) {
	return getPage(currentPage, 1);
}

function getPreviousPage(currentPage) {
	return getPage(currentPage, -1);
}

function initNavigation(currentPage, elem, diffX) {

	$.touch.triggerMouseEvents = true;
	$.touch.ready(

	function() {

		$(elem).touchable(
				{

					gesture : function(e, history) {
						if (history.match({
							finger : '1',
							deltaX : '<-' + diffX,
							time : '1..100'
						})) {
							window.location.replace("./"
									+ getNextPage(currentPage));
						} else if (history.match({
							finger : '1',
							deltaX : '>' + diffX,
							time : '1..100'
						})) {
							window.location.replace("./"
									+ getPreviousPage(currentPage));
						}
					}

				});

	});
	$.touch.preventDefault = false;

}
